package com.scorpio.sdk.constant;


import com.alibaba.fastjson.JSONObject;
import com.scorpio.common.exception.CustomException;
import com.scorpio.common.utils.MathUtil;
import com.scorpio.common.utils.XmlUtils;
import com.scorpio.common.utils.sign.Base64;
import com.scorpio.common.utils.spring.SpringUtils;
import com.scorpio.sdk.dto.WXMiniPayPackge;
import com.scorpio.sdk.service.WXPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class WeiXinConstant {
    private static final Logger logger = LoggerFactory.getLogger(WeiXinConstant.class.getClass());

    //微信统一下单地址
    public static final String WEIXIN_PAY_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/app";
    //扫码支付
    public static final String WEIXIN_PAY_NATIVE_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
    //JSAPI支付
    public static final String WEIXIN_PAY_JPAPI_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
    //关闭订单
    public static final String WEIXIN_PAY_CLOSE_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/%s/close";
    //微信APPID
    public static final String WEIXIN_PAY_APPID = "weixin:pay_appid";
    //微信商户号
    public static final String WEIXIN_PAY_MCH_ID = "weixin:pay_mch_id";
    //微信支付密钥
    public static final String WEIXIN_PAY_SECRET = "weixin:pay_secret";
    //微信支付通知
    public static final String WEIXIN_PAY_NOTIFY_URL = "weixin:pay_notify_url";
    //微信支付 证书编号
    public static final String WEIXIN_PAY_SERIAL_NUMBER = "weixin:pay_serial_number";


    //微信公众号APPID
    public static final String WEIXIN_TENCENT_APPID = "weixin:tencent_appid";
    //微信公众号密钥
    public static final String WEIXIN_TENCENT_SECRET = "weixin:tencent_secret";
    //微信公众号 获取access_token
    public static final String WEIXIN_TENCENT_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";
    //微信公好 获取jsapi_ticket
    public static final String WEIXIN_TENCENT_GETTICKET = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
    //获取微信用户列表
    public static final String WEIXIN_USER_LIST_URL = "https://api.weixin.qq.com/cgi-bin/user/get";
    //获取用户详情列表
    public static final String WEIXIN_USER_DETAILS_URL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget";
    //微信公众号推送模版消息
    public static final String WEIXIN_SEND_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send";

    //微信公众号-开会员消息模版
    public static final String WEIXIN_MSG_OPEN_VIP_TEMPLATE_ID = "weixin:msg_open_vip_template_id";
    //微信公众号-开通会员审核
    public static final String WEIXIN_MSG_AGENT_OPEN_VIP_TEMPLATE_ID = "weixin:msg_agent_open_vip_template_id";
    //微信公众号-新注册用户消息模版
    public static final String WEIXIN_MSG_NEW_USER_TEMPLATE_ID = "weixin:msg_new_user_template_id";
    //微信消息提醒发送到的WxOpenId
    public static final String WEIXIN_TO_WX_OPEN_ID = "weixin:to_wx_open_id";


    //小程序登录url
    public static final String MINI_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";

    //小程序 的app_id
    public static final String MINI_APP_ID = "s_config:mini_app_id";

    //小程序 的secret
    public static final String MINI_SECRET = "s_config:mini_secret";

    /**
     * 获取小程序支付签名数据
     *
     * @param appid    应用ID
     * @param prepayid 预支付交易会话ID
     * @return
     */
    public static WXMiniPayPackge getMiniPaySignJson(String appid, String prepayid) {
        WXMiniPayPackge packge = new WXMiniPayPackge();
        packge.timeStamp = "" + System.currentTimeMillis() / 1000;
        packge.nonceStr = MathUtil.getRandLetter(32).toUpperCase();
        packge.packageValue = "prepay_id=" + prepayid;
        packge.signType = "RSA";
        StringBuffer sb = new StringBuffer();
        sb.append(appid).append("\n").append(packge.timeStamp).append("\n").append(packge.nonceStr).append("\n").append(packge.packageValue).append("\n");
        WXPayService payService = SpringUtils.getBean(WXPayService.class);
        String sign = sb.toString();
        packge.paySign = payService.sign(sign);
        return packge;
    }

    public static void disposeResult(String url, JSONObject json, String data, boolean isShowError) {
        if ("0".equals(json.getString("code"))) {
        } else {
            logger.error("---------------------------");
            logger.error("接口请求失败:" + data);
            logger.error("请求地址:" + url);
            logger.error("请求数据:" + json.toJSONString());
            if (isShowError) throw new CustomException("处理结果异常");
        }
    }

    //支付回调返回成功
    public static final String getPaySuccessResult() {
        Map<String, String> result = new HashMap<>();
        result.put("return_code", "SUCCESS");
        result.put("return_msg", "OK");
        return XmlUtils.toXml(result);
    }

    public static final String getPayFAILResult() {
        Map<String, String> result = new HashMap<>();
        result.put("return_code", "FAIL");
        result.put("return_msg", "失败");
        return XmlUtils.toXml(result);
    }

    //格式化消息模版参数
    public static final JSONObject toMsgValueTemplate(String value) {
        JSONObject json = new JSONObject();
        json.put("value", value);
        json.put("color", "#173177");
        return json;
    }
}
